package com.zhugang.week04;

import java.util.Arrays;

/**
 * @program algorithms
 * @description: canMakeArithmeticProgression
 * @author: chanzhugang
 * @create: 2022/06/30 08:41
 */
public class CanMakeArithmeticProgression {

    /**
     * 1502. 判断能否形成等差数列
     *
     * @param arr
     * @return
     */
    public boolean canMakeArithmeticProgression(int[] arr) {
        // 排序数组，计算相邻元素差是否相等
        Arrays.sort(arr);
        int diff = arr[1] - arr[0];
        for (int i = 2; i < arr.length; i++) {
            if (arr[i] - arr[i - 1] != diff) {
                return false;
            }
        }
        return true;
    }
}